package com.mate.cloud.xml;

import org.dom4j.Document;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Tests that XSLT works correctly
 * <a href="mailto:jstrachan@apache.org">James Strachan </a>
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2024/6/2 20:41
 * @updateUser: MI
 * @updateTime: 2024/6/2 20:41
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@SpringBootTest
public class XSLTTests extends AbstractTestCase {

    /**
     * 测试 xslt 代码
     *
     * @throws Exception
     */
    @Test
    public void testTransform() throws Exception {

        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("classpath:xml/nitf/ashtml.xsl");

        Assert.isTrue(resource.exists(), "xsl file not found");
        // 2、将xml解析为树
        Document transformedDoc = transform("classpath:xml/nitf/ashtml.xsl");

        // log( transformedDoc.asXML() );
        assertTrue("Transformed Document is not null", transformedDoc != null);

        List h1List = transformedDoc.selectNodes("/html//h1");

        assertTrue("At least one <h1>", h1List.size() > 0);

        List pList = transformedDoc.selectNodes("//p");

        assertTrue("At least one <p>", pList.size() > 0);
    }

    protected Document transform(String xsl) throws Exception {
        assertTrue("Document is not null", document != null);

        // load the transformer
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(getFile(xsl)));

        // now lets create the TrAX source and result
        // objects and do the transformation
        Source source = new DocumentSource(document);
        DocumentResult result = new DocumentResult();
        transformer.transform(source, result);

        return result.getDocument();
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        document = getDocument("classpath:xml/nitf/sample.xml");
    }
    @Override
    protected File getFile(String xsl) {
        // Add logic to load the XSL file from the classpath or file system
        // 添加逻辑以从类路径或文件系统加载 XSL 文件
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource(xsl);
        try {
            return resource.getFile();
        } catch (IOException e) {
            throw new RuntimeException("无法加载 XSL 文件", e);
        }
    }
    @Override
    protected Document getDocument(String xmlPath) throws Exception {
        SAXReader reader = new SAXReader();
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource(xmlPath);
        return reader.read(resource.getInputStream());
    }
}
